#!/bin/bash

# description: install hadoop

# iptables
# 命令行参数指定网络地址、子网掩码，如：192.168.1.0 24
iptablesRule="iptables -A INPUT -p tcp -s $1/$2 --dport 1024:65535 -j ACCEPT"
saveiptables="service iptables save"
hadoopConfigPath="/etc/hadoop/"
hadoopDataPath="/data"

# master的初始化工作
iptables -F
$iptablesRule
$saveiptables

if [ -d "$hadoopConfigPath" ]; then
	rm -rf $hadoopConfigPath
fi

mkdir $hadoopConfigPath
cp ./hadoop/* $hadoopConfigPath  -r
chown hadoop:hadoop $hadoopConfigPath  -R


if [ ! -d "$hadoopDataPath" ]; then
	mkdir $hadoopDataPath
fi
chown hadoop:hadoop $hadoopDataPath  -R

slaveHosts=("dev5" "dev6")

# pre
for slave in ${slaveHosts[*]}
do
	ssh root@$slave "iptables -F &&  $iptablesRule && $saveiptables && exit 0"
done

# scp hadoop config file
for slave in ${slaveHosts[*]}
do
	rsync -a -v -z -e ssh --delete $hadoopConfigPath $slave:$hadoopConfigPath
	# scp -r /etc/hadoop/  root@$slave:/etc/hadoop/
done

# post
for slave in ${slaveHosts[*]}
do
	ssh root@$slave "/root/_hadoop_config_post.sh"
done

# 复制jmx文件
if [ -f "/data/jmxremote.password" ]; then
	 rm -f  /data/jmxremote.password
fi
cp ./jmx/jmxremote.password  /data/
if [ -f "/data/jmxremote.access" ]; then
	 rm -f  /data/jmxremote.access
fi
cp ./jmx/jmxremote.access  /data/


echo "hadoop install all end"

